package jmine.tec.utils.io;

import java.io.IOException;
import java.io.ObjectStreamException;
import java.io.OutputStream;
import java.io.Serializable;

/**
 * {@link OutputStream} that just absorbs whatever is written to it
 * 
 * @author takeshi
 */
public final class NullOutputStream extends OutputStream implements Serializable {

    /**
     * Inner instance holder class
     * 
     * @author takeshi
     */
    private static final class InstanceHolder {
        private static final NullOutputStream INSTANCE = new NullOutputStream();

        /**
         * just to satisfy CS
         */
        private InstanceHolder() {
        }

    }

    /**
     * Returns the single instance
     * 
     * @return {@link OutputStream}
     */
    public static OutputStream getInstance() {
        return InstanceHolder.INSTANCE;
    }

    /**
     * Empty privat constructor
     */
    private NullOutputStream() {
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void write(int b) throws IOException {
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void write(byte[] b) throws IOException {
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void write(byte[] b, int off, int len) throws IOException {
    }

    /**
     * Replaces an instance on desserialization
     * 
     * @return {@link #getInstance()}
     * @throws ObjectStreamException e
     */
    protected Object readResolve() throws ObjectStreamException {
        return getInstance();
    }
}
